package dynamicFolders;

import java.sql.ResultSet;
import java.util.Iterator;
import java.util.Vector;

import main.Main;
import database.Database;
import exceptions.IncompleteException;

public class View {

	/**
	 * @author Jeroen Mabille
	 */

	Vector<Column> columns;

	public View() {
		this.columns = new Vector<Column>();
	}

	public void addColumn(Column c) throws Exception {
		if (c == null) {
			throw new Exception("Column is null");
		}
		this.columns.add(c);
	}
	
	public void insertColumn(Column c,  int index){
		columns.remove(index);
		columns.insertElementAt(c, index);
	}

	public int getNrColumns() {
		return this.columns.size();
	}

	public String getColumns() throws IncompleteException {
		String columnNames = "";
		Iterator<Column> it = columns.iterator();

		if (columns.isEmpty()) {
			throw new IncompleteException("No columns filled in.");
		}

		Column c = it.next();
		columnNames = columnNames + c.getColumnName();
		if (c.getTitle() != null) {
			columnNames = columnNames + " \"" + c.getTitle() + "\"";
		}

		while (it.hasNext()) {
			c = it.next();
			columnNames += ", ";
			columnNames = columnNames + c.getColumnName();
			if (c.getTitle() != null) {
				columnNames = columnNames + " \"" + c.getTitle() + "\"";
			}
		}

		return columnNames;
	}

	public Vector<String> generateHeader() {
		Vector<String> header = new Vector();
		if(this.columns.size() != 0){
			Iterator<Column> it = columns.iterator();
			Column c = it.next();
			if (c != null) {
				header.add(c.getColumnName());
	
			}
			while (it.hasNext()) {
				c = it.next();
				header.add(c.getColumnName());
	
			}
		}
		return header;
	}

	public Vector<Vector<String>> generateViewBody() {
		Vector<Vector<String>> rowData = new Vector();
		if(this.columns.size() != 0){
			rowData.add(generateTableLine());
			rowData.add(generateHeader());
			rowData.add(generateConstraintLine());
		}
		return rowData;
	}

	public Vector<String> generateTableLine() {
		Vector<String> tableRow = new Vector();
		Iterator<Column> it = columns.iterator();
		Column c = it.next();
		if (c != null) {
			tableRow.add(c.getTableName());
		}
		while (it.hasNext()) {
			c = it.next();
			tableRow.add(c.getTableName());
		}
		return tableRow;
	}

	public Vector<String> generateConstraintLine() {
		Vector<String> constraintRow = new Vector();
		Iterator<Column> it = columns.iterator();
		Column c = it.next();
		if (c != null) {
			constraintRow.add(c.getConstraintString());
		}
		while (it.hasNext()) {
			c = it.next();
			constraintRow.add(c.getConstraintString());
		}
		return constraintRow;
	}

	public String getTables() throws IncompleteException {
		String tables = "";
		Iterator<Column> it = columns.iterator();
		if (columns.isEmpty()) {
			throw new IncompleteException("No tablename filled in.");
		}
		Column c = it.next();
		tables += c.getTableName();

		while (it.hasNext()) {
			c = it.next();
			if (!tables.contains(c.getTableName())) {
				tables = tables + ", " + c.getTableName();
			}
		}

		return tables;
	}

	public String getConstraints() throws IncompleteException {
		String cons = " WHERE ";
		int count = 0;
		boolean needCons = false;
		Iterator<Column> it = columns.iterator();
		if (columns.isEmpty()) {
			throw new IncompleteException("No columns added.");
		}
		while (it.hasNext()) {
			Column c = it.next();
			if (c.hasConstraints()) {
				if (count == 0) {
					cons += c.Constraints();
				} else {
					cons += " AND " + c.Constraints();
				}
				count++;
				needCons = true;
			}
		}
		if (needCons)
			return cons;
		else
			return "";
	}

	public String getOrder() throws IncompleteException {
		if (columns.isEmpty()) {
			throw new IncompleteException("No columns added.");
		}
		String order = " Order By ";
		boolean needOrder = false;
		int count = 0;
		Iterator<Column> it = columns.iterator();
		while (it.hasNext()) {
			Column c = it.next();
			if (c.isOrderBy()) {
				if (count == 0) {
					order += c.getColumnName();
				} else {
					order += ", " + c.getColumnName();
				}
				if (c.isDesc()) {
					order += " Desc";
				}
				count++;
				needOrder = true;
			}
		}
		if (needOrder)
			return order;
		else
			return "";
	}

	public String createQuerry() {
		String querry = "Select ";
		try {
			querry += this.getColumns();
		} catch (IncompleteException e) {
			System.out.println(e.getWhat());
		}
		querry += " From ";
		try {
			querry += this.getTables();
		} catch (IncompleteException e) {
			System.out.println(e.getWhat());
		}
		try {
			querry += this.getConstraints();
		} catch (IncompleteException e) {
			System.out.println(e.getWhat());
		}
		try {
			querry += this.getOrder();
		} catch (IncompleteException e) {
			System.out.println(e.getWhat());
		}

		return querry + ";";

	}
	
	public void deleteColumn(int i){
		this.columns.remove(i);
	}

	public Vector<Vector<String>> querryDb() throws Exception {
		String querry = this.createQuerry();

		// TODO querry db generated querry, return resultset. need database name
		// etc...

		ResultSet rs = null;
		try {
			rs = Main.db.getSomething(querry);
		} catch (Exception e) {
			e.getMessage();
			e.printStackTrace();
		}

		Vector<Vector<String>> rowData = new Vector();

		while (rs.next()) {
			Vector<String> row = new Vector();
			Iterator<Column> it = columns.iterator();
			Column c = it.next();
			if (c != null) {
				row.add(rs.getString(c.getColumnName()));
			}
			while (it.hasNext()) {
				c = it.next();
				row.add(rs.getString(c.getColumnName()));
			}
			rowData.add(row);
		}

		return rowData;
	}

}
